#include <QCoreApplication>

#include <QAudioFormat>
#include <QAudioSink>
#include <QDir>
#include <QFile>
#include <QMediaDevices>

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    qDebug() << QDir::current();

    auto audio_devics = QMediaDevices::audioOutputs();
    for (auto &dev : audio_devics) {
        qDebug() << "audio output id " << dev.id() << ", desc "
                 << dev.description() << ", preferred fmt "
                 << dev.preferredFormat() << ", sample fmts "
                 << dev.supportedSampleFormats() << ", sample rate ["
                 << dev.minimumSampleRate() << ", " << dev.maximumSampleRate()
                 << "], channel [" << dev.minimumChannelCount() << ", "
                 << dev.maximumChannelCount() << "]";
    }

    QFile inputFile;
    inputFile.setFileName("./pcm_audio_sink.pcm");
    if (!inputFile.open(QIODevice::ReadOnly)) {
        qDebug() << "打开错误";
        return 1;
    }

    QAudioFormat format;
    // Set up the format, eg.
    format.setSampleRate(44100);
    format.setChannelCount(2);
    format.setSampleFormat(QAudioFormat::Int16);


    QAudioDevice info(QMediaDevices::defaultAudioOutput());
    if (!info.isFormatSupported(format)) {
        qWarning()
            << "Raw audio format not supported by backend, cannot play audio.";
        return 1;
    }

    qDebug() << "use audio device " << info.description();

    auto audio = new QAudioSink(info, format);
    audio->start(&inputFile);

    return a.exec();
}
